<?php

date_default_timezone_set("Asia/Jakarta");

function pub_conf($type) {
    $confObj = null;

    switch ($type) {
        case "buletin" :
            $confObj = new stdClass();
            $confObj->pubDir = "buletin";
            $confObj->pubTitle = "Buletin PS Edisi ";
            $confObj->pubFile = "Buletin_PS_Edisi_";
            break;
        case "pb" :
            $confObj = new stdClass();
            $confObj->pubDir = "pb";
            $confObj->pubTitle = "Policy Brief PS : ";
            $confObj->pubFile = "Policy_Brief_PS_";
            break;
        case "regulasi" :
            $confObj = new stdClass();
            $confObj->pubDir = "regulasi";
            $confObj->pubTitle = "Regulasi PS : ";
            $confObj->pubFile = "Regulasi_PS_";
            break;
    }

    return $confObj;
}

function pub_fetchall($type) {
    $result = Array();
    $pubConf = pub_conf($type);
    $folderPath = APPPATH . "data/pub/" . $pubConf->pubDir . "/";
    $fileNames = get_filenames($folderPath);
    $excludedFiles = Array("index.html");
    $names = Array();
    foreach ($fileNames as $fileName) {
        if (!in_array($fileName, $excludedFiles)) {
            $fa = explode($pubConf->pubFile, $fileName);
            if (count($fa) < 2) {
                unlink($folderPath . $fileName);
            } else {
                $ff = explode(".", $fa[1], 2);
                if ((count($ff) > 1) && ($ff[1] == "pdf")) {
                    array_push($names, $ff[0]);
                } else {
                    if ((count($ff) > 1) && ($ff[1] == "prev_.pdf")) {
                        //..
                    } else {
                        unlink($folderPath . $fileName);
                    }
                }
            }
        }
    }
    sort($names);
    foreach ($names as $name) {
        $row = new stdClass();
        $row->url = base_url("application/data/pub/" . $pubConf->pubDir . "/" . $pubConf->pubFile . $name . ".pdf");
        $row->prev = file_exists($folderPath . $pubConf->pubFile . $name . ".prev_.pdf") ? base_url("application/data/pub/" . $pubConf->pubDir . "/" . $pubConf->pubFile . $name . ".prev_.pdf") : $row->url;
        $row->title = $pubConf->pubTitle . str_replace("_", " ", $name);
        $row->name = $name;
        array_push($result, $row);
    }
    return $result;
}

function res_url($url, $toLower = true) {
    $result = base_url("application/res/" . $url);
    if ($toLower) {
        $result = strtolower($result);
    }

    return $result;
}

function temp_url($url, $toLower = true) {
    $result = base_url("application/temp/" . $url);
    if ($toLower) {
        $result = strtolower($result);
    }

    return $result;
}

function tempFolderCleanUp($folder) {
    $numHour = 1;
    $folderPath = APPPATH . "temp/" . $folder . "/";
    $fileNames = get_filenames($folderPath);
    $excludedFiles = Array("index.html");
    foreach ($fileNames as $fileName) {
        if (!in_array($fileName, $excludedFiles)) {
            $fa = explode("_", $fileName);
            if (count($fa) < 2) {
                unlink($folderPath . $fileName);
            } else {
                $timestamp = $fa[0];
                if (intval($timestamp) > 1) {
                    if (intval($timestamp) < (intval(time()) - (3600 * $numHour))) {
                        unlink($folderPath . $fileName);
                    }
                } else {
                    unlink($folderPath . $fileName);
                }
            }
        }
    }
}

// list generator for SITEMAP
function generateList($root, $depth = 2, $menuHL, $isMain = true, $divideArr = Array()) {
    $li = "";
    $divIdx = 0;
    if (count($divideArr) > 0) {
        $spliceLength = $divideArr[$divIdx];
        $splicedArr = new stdClass();
        $splicedArr->menu = Array();
        $i = 0;
        $idx = 1;
        foreach ($root->menu as $menu) {
            $i++;
            if (($i <= $spliceLength) || ($spliceLength < 1)) {
                array_push($splicedArr->menu, $menu);
            } else {
                $splicedArr->idx = "_" . $idx;
                $li .= generateList($splicedArr, $depth, $menuHL);
                $splicedArr->menu = Array();
                array_push($splicedArr->menu, $menu);
                $i = 0;
                $idx++;
                $divIdx++;
                if ($divIdx < count($divideArr)) {
                    $spliceLength = $divideArr[$divIdx];
                } else {
                    $spliceLength = 0;
                }
            }
        }
        if (count($splicedArr->menu) > 0) {
            $splicedArr->idx = "_" . $idx;
            $li .= generateList($splicedArr, $depth, $menuHL);
        }
    } else {
        $li .= "<ul class='" . ($isMain ? "nav-main" : "nav-sub") . ($root->idx ? (" " . $root->idx) : "") . "'>";
        foreach ($root->menu as $menu) {
            if ($menu["title"] == $menuHL) {
                $li .= "<li class='nav-hl'>";
            } else {
                $li .= "<li>";
            }
            $li .= "<a href='" . site_url($menu["url"]) . "'>";
            $li .= "<span>" . $menu["title"] . "</span>";
            $li .= "</a>";
            if (($menu->menu) && ($depth > 1)) {
                $li .= generateList($menu, ($depth - 1), $menuHL, false);
            }
            $li .= "</li>";
        }
        $li .= "</ul>";
    }
    return $li;
}

// sidenav generator
function generateSideNav($arr, $depth = 2, $isMain = true) {
    $li = "";
    $li .= "<ul class='" . ($isMain ? "sidenav-main" : "sidenav-sub") . "'>";
    foreach ($arr as $list) {
        $li .= "<li>";
        $li .= "<a href='" . site_url($list["url"]) . "'>";
        $li .= "<span>" . $list["title"] . "</span>";
        $li .= "</a>";
        if (isset($list["sub"])) {
            $li .= generateSideNav($list["sub"], ($depth - 1), false);
        }
        $li .= "</li>";
    }
    $li .= "</ul>";
    return $li;
}

?>
